//
//  SKTabBar.m
//  loveKangaroo
//
//  Created by SeeKool on 15/3/3.
//  Copyright (c) 2015年 SeeKool Inc. All rights reserved.
//

#import "SKTabBar.h"
#import "SKTabBarButton.h"

@interface SKTabBar ()
@property (nonatomic, strong) NSMutableArray *subBtns;
@property (nonatomic, strong) UIButton *currentSelBtn;
@property (nonatomic, weak) UIButton *creatNewBtn;
@end
@implementation SKTabBar

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor redColor];
        //添加加号按钮
        [self addCreateNewBtn];
    }
    return self;
}
/**
 *  添加加号按钮
 */
- (void)addCreateNewBtn{
    //创建按钮
    UIButton *creatNewBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [creatNewBtn setTitle:@"+" forState:UIControlStateNormal];
    //添加
    [self addSubview:creatNewBtn];
    //给加号按钮添加点击事件
    [creatNewBtn addTarget:self action:@selector(creatNewBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    self.creatNewBtn = creatNewBtn;
}
/**
 *  加号按钮点击事件
 */
- (void)creatNewBtnOnClick{
//    if ([self.delegate respondsToSelector:@selector(tabBarDidClickCreatNewBtn:)]) {
//        [self.delegate tabBarDidClickCreatNewBtn:self];
//    }
    //通知首页加号按钮被点击
    [[NSNotificationCenter defaultCenter] postNotificationName:@"creatNewBtnOnClick" object:nil userInfo:nil];
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    NSUInteger count = self.subBtns.count;
    CGFloat BtnWidth = self.width / (count + 1);
    CGFloat BtnHeight = self.height;
    //1.选项卡按钮的frame
    for (int i = 0; i < count; i++) {
        //1.取出选项卡按钮
        SKTabBarButton *btn = self.subBtns[i];
        //2.设置tag
        btn.tag = i;
        //3.设置frame
        if (i >= count * 0.5) {
            btn.frame = CGRectMake((i + 1) * BtnWidth, 0, BtnWidth, BtnHeight);
        }else {
            btn.frame = CGRectMake(i * BtnWidth, 0, BtnWidth, BtnHeight);
        }
        
    }
    //2.加号按钮的frame
    self.creatNewBtn.frame = CGRectMake(count * 0.5 * BtnWidth, 0, BtnWidth, BtnHeight);
}

- (void)addOneBtnWithItem:(UITabBarItem *)item{
    //1.创建按钮
    SKTabBarButton *btn = [SKTabBarButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    //2.给按钮数据模型
    btn.item = item;
    //3.添加选项卡按钮到自定义tabBar
    [self addSubview:btn];
    //4.添加当前的按钮到选项卡数组中
    [self.subBtns addObject:btn];
    //5.判断是否是第一个按钮
    if (self.subBtns.count == 1) {
        //默认选中第一个
        [self btnOnClick:btn];
    }
}
- (void)btnOnClick:(UIButton *)btn{
    self.currentSelBtn.selected = NO;
    btn.selected = YES;
    if ([self.delegate respondsToSelector:@selector(tabBarDidSelectedFromIndex:toIndex:)]) {
        [self.delegate tabBarDidSelectedFromIndex:self.currentSelBtn.tag toIndex:btn.tag];
    }
    self.currentSelBtn = btn;
}
#pragma mark - 懒加载
- (NSMutableArray *)subBtns{
    if (_subBtns == nil) {
        _subBtns = [NSMutableArray array];
    }
    return _subBtns;
}

@end
